<?php
include_once(dirname(__FILE__) . "/libs/nms_html_scripts.php");
include_once(dirname(__FILE__) . "/libs/nms_html_views.php");
include_once(dirname(__FILE__) . "/model/nms_interface_model.php");
include_once(dirname(__FILE__) . "/libs/config.php");
include_once(dirname(__FILE__) . "/model/nms_alarm_model.php");
function draw_group_phone(){
		html_master_script_filter();
		html_group_phone_script();
		html_group_phone_script_reload();
		draw_group_phone_header();
		if(empty($_REQUEST['group_name']))
		{
			$group_name = "";
		}
		else {
			$group_name=$_REQUEST['group_name'];
		}
		$url_post= "?run=group&tab=group_phone&group_name=".$group_name."";
		if (empty($_GET['group_name'])) {
			$id_group="";
		}
		else{
			$id_group =  $_GET['group_name'];
		}
		?>
				<script type="text/javascript">
		    	$("#id_group").val('<?php echo $id_group;?>');
		    	</script>
	 <?php 
 		draw_group_phone_main($url_post);
	 if(isset($_POST['Remove']))
	 {
	 	$action = $_POST['phone'];
		array_map('intval',$action);
		$action = implode(',',$action);
		$action = explode(",", $action);
		for($x=0;$x<count($action);$x++)
		{
			$id=(int)$action[$x];
			remove_add_groupinter_groupphone($id);
		}
		echo '<script>
		 			reLoad1();
		 		</script>';
	 }
	 if(isset($_POST['RemoveAll']))
	 {
	 	$id_name = $_REQUEST['group_name'];	
	 	$output =check_id_phone_from_group_name($id_name);
	 	foreach ($output as $item){
	 		remove_add_groupinter_groupphone($item['intercontact_id']);
	 	}
	 	echo '<script>
		 			reLoad1();
		 		</script>';
	 }
	 if(isset($_POST['AddAll']))
	 {
	 	$id_name = $_REQUEST['group_name'];
	 	$query1 = 'SELECT contact_group_id from plugin_nms_contactdetail where interface_group_id ="'.$id_name.'" ';
		$data = db_fetch_assoc($query1);
		$group_phone = getContactnotused($data);
		foreach ($group_phone as $setitem){
			add_groupinter_groupphone($id_name, $setitem['id']);
		}
		echo '<script>
		 			reLoad1();
		 		</script>';
	 }
	 if(isset($_POST['Add']))
	 {
	 	$action = $_POST['phone1'];
		array_map('intval',$action);
		$action = implode(',',$action);
		$action = explode(",", $action);
		$group_inter =$_REQUEST['group_name'];
		for($x=0;$x<count($action);$x++)
		{
			$id=(int)$action[$x];
			if($id ==0){
				echo '<script>
		 			reLoad1();
		 		</script>';
			}
			else{
			add_groupinter_groupphone($group_inter, $id);
			}
			
		}
		echo '<script>
		 			reLoad1();
		 		</script>';
	 }
	 
 }
 
	function draw_group_phone_header(){
		html_master_header_start("Group Interface Management");
	?>
			<div>&nbsp;</div>
				<table border="0">
					<tr>
						<td ><h3>Group Interface:    </h3></td>
						<td>
						<select  onchange="val()" id="id_group">
						<option value=""></option>
						<?php
						$hosts = db_fetch_assoc("select interface_group_id,group_name from plugin_nms_groupname order by group_name");
						if (sizeof($hosts) > 0) {
							foreach($hosts as $set) {
								echo "<option value='" . $set["interface_group_id"] . "'"; echo ">" . htmlspecialchars($set["group_name"]) . "</option>\n";
							}
						}
						?>
		        		</select>
		        		</td>
				  	 	<td>
				  	 		<h4>Refresh time:</h4>
				  	 	</td>
				  	 	<td>
				  	 		<h4><div id ="countdown_time2"></div></h3>
				  	 	</td> 					
					</tr>
				</table>
				
 <?php 
 		html_alarm_script_time();
 		html_master_header_end();
	}
	function draw_group_phone_main($url_post){
?>
<form action="<?php echo $url_post ?>" method ="post"enctype="multipart/form-data">
        <div id ="id_group_phone">
        <div id ="id_group_left">
        </div>
        <div id ="id_group_left_center">
        		<div>&nbsp;</div>
        		<table width="50%" border="0">
			    	<tr>
			    	<td > <h3> Group Interface Used:  </h3></td>
		    		</tr>
		    	</table>
        		<table width="100%" id="featured">
					<tr class="cls_master_table_title" align="center"  >
						<th width="1%" scope="col" ></th>
						<th width="3%" scope="col">No.</th>
						<th width="20%" scope="col" >Group Phone</th>
					</tr>
				<?php 
				$sql_where  ="";
				if (!empty($_GET['group_name'])) {
					$sql_where .="and b.interface_group_id =".$_GET['group_name']."";
				}else{
					$sql_where .="and b.interface_group_id =0";
				}
				$query = "SELECT count(a.intercontact_id)
							FROM plugin_nms_contactdetail a, plugin_nms_groupname b
							where a.interface_group_id = b.interface_group_id ".$sql_where." ";
				$display =db_fetch_cell($query);
				$querydata="SELECT a.intercontact_id, a.contact_group_id , b.group_name
							FROM plugin_nms_contactdetail a , plugin_nms_groupname b
							where  a.interface_group_id = b.interface_group_id ".$sql_where."
							order by a.intercontact_id  limit 0,$display";
				$result=db_fetch_assoc($querydata);
				$stt=1;
				foreach($result as $set) {
				?>
					<tr <?php if($stt %2==0) {echo 'class="xanhhong"';}
				 					else {echo 'class="xanhlot"';}?> >
						<td align="center">&nbsp;<input type="checkbox" value="<?echo $set['intercontact_id'] ?>"id="phone[]" name="phone[]" class='cls-alarm-table-checkbox'/> 
					 	<td align="center">&nbsp;<?php  echo $stt++; ?></td>
						<td align="center"><?php 
								$contact_name =getcontactgroupname($set['contact_group_id']);
						   		echo "$contact_name";
						 ?></td>
						</td>
					</tr>
				<?php 
				}
				?>
				</table>
        </div>
        <div id ="id_group_right_center">
        		<div>&nbsp;</div>
        		<div>&nbsp;</div>
        		<table width="100%" border="0">
			    	<tr>
			    	<td align="center" ><input type="submit" name="RemoveAll" id ="btnAdd" value="&gt;&gt;"/></td>
		    		</tr>
		    	</table>
		    	<table width="100%" border="0">
			    	<tr>
			    	<td align="center" ><input type="submit" name="Remove" id ="btnAdd" value="&gt;"/></td>
		    		</tr>
		    	</table>
		    	<table width="100%" border="0">
			    	<tr>
			    	<td align="center" ><input type="submit" name="Add" id ="btnDel" value="&lt;"/></td>
		    		</tr>
		    	</table>
		    	<table width="100%" border="0">
			    	<tr>
			    	<td align="center" ><input type="submit" name="AddAll" id ="btnDel" value="&lt;&lt;"/></td>
		    		</tr>
		    	</table>
       	</div>
        <div id ="id_group_right">
        		<div>&nbsp;</div>
        		<table width="50%" border="0">
			    	<tr>
			    	<td ><h3> Group Interface :  </h3></td>
		    		</tr>
		    	</table>
        		<table width="100%" id="featured">
					<tr class="cls_master_table_title" align="center" >
						<th width="1%" scope="col" ></th>
						<th width="3%" scope="col">No.</th>
						<th width="20%" scope="col" >Group Phone</th>
					</tr>
				<?php 		
					$sql_where1  ="";
						if (!empty($_GET['group_name'])) {
							$sql_where1 .=" interface_group_id =".$_GET['group_name']."";
						}else{
							$sql_where1 .="";
						}
						if(empty($_REQUEST['group_name'])){
							
						}else{
						$query1 = 'SELECT contact_group_id from plugin_nms_contactdetail where '.$sql_where1.' ';
						$data = db_fetch_assoc($query1);
						$group_phone = getContactnotused($data);
	     					 if (sizeof($group_phone) > 0) {
								$stt1=1;
						     foreach($group_phone as $set1) {
				?>
					<tr <?php if($stt1 %2==0) {echo 'class="xanhhong"';}
				 					else {echo 'class="xanhlot"';}?> >
						<td align="center">&nbsp;<input type="checkbox" value="<?echo $set1['id'] ?>"id="phone1[]" name="phone1[]" class='cls-alarm-table-checkbox'/> 
					 	<td align="center">&nbsp;<?php  echo $stt1++; ?></td>
						<td align="center"><?php 
								//$contact_name =getcontactgroupname($set1['contact_group_id']);
						   		//echo "$contact_name";
						   		echo $set1['group'];
						 ?></td>
					</tr>
				<?php 
					}
				   }
				 }
				?>
				</table>
        </div>
        <div class="vide"></div>
    	</div>
    	</form>
<?php 
	}
 ?>